﻿using SLClient;
using SLClient.ViewModels;
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;

namespace SLClient.Views
{
    public partial class LoginView : UserControl
    {
        private readonly UserAgreeWindow _userAgreeWindow = new UserAgreeWindow();
        private EventHandler _LoginSuccess;

        public event EventHandler LoginSuccess
        {
            add
            {
                EventHandler handler2;
                EventHandler loginSuccess = this._LoginSuccess;
                do
                {
                    handler2 = loginSuccess;
                    EventHandler handler3 = (EventHandler)Delegate.Combine(handler2, value);
                    loginSuccess = Interlocked.CompareExchange<EventHandler>(ref this._LoginSuccess, handler3, handler2);
                }
                while (loginSuccess != handler2);
            }
            remove
            {
                EventHandler handler2;
                EventHandler loginSuccess = this._LoginSuccess;
                do
                {
                    handler2 = loginSuccess;
                    EventHandler handler3 = (EventHandler)Delegate.Remove(handler2, value);
                    loginSuccess = Interlocked.CompareExchange<EventHandler>(ref this._LoginSuccess, handler3, handler2);
                }
                while (loginSuccess != handler2);
            }
        }

        public LoginView()
        {
            this.InitializeComponent();
            base.DataContext=new LoginViewModel(ref this._userAgreeWindow);
            this._userAgreeWindow.Closed += new EventHandler(this.UserAgreeWindowClosed);
        }

        private void btLogin_Click(object sender, RoutedEventArgs e)
        {
            (base.DataContext as LoginViewModel).LoginProcess();
        }

        private void UserAgreeWindowClosed(object sender, EventArgs e)
        {
            if (((sender as UserAgreeWindow).DialogResult == true) && (this._LoginSuccess != null))
            {
                this._LoginSuccess(this, null);
            }
        }

        private void tbPassWord_PasswordChanged(object sender, RoutedEventArgs e)
        {
            (base.DataContext as LoginViewModel).PassWord = tbPassWord.Password;
        }
    }
}
